<template>
	<view>
		<view class="TopNav">
			<p @tap="navTab(1)" :class="{'acts':state == 1}" :ripple="true">收货地址</p>
			<p @tap="navTab(0)" :class="{'acts':state == 0}" :ripple="true">发货地址</p>
		</view>
		<view v-if="data != '' " class="p30 ">
			<view class="mains">
				<view class="list" v-for="(v, i) in data" :key="v.id">
					<!-- <u-swipe-action :show="v.show" :index="i" @tap="remove(i,v.id)" @open="open" :options="options"> -->
					<view @tap.stop @tap="select(v)" class="listBox">
						<p class="p1 two">
							<image v-if="v.default == true" class="vm actIcon" src="../../static/image/moren@2x.png" mode=""></image>
							{{v.address}} {{v.doorplate}}
						</p>
						<p class="p2 one">
							{{v.name}} {{v.mobile}}
						</p>
						<view @tap.stop @tap="GoSet(v)" class="go">
							<image src="../../static/image/icon_bianji@2x.png"></image>
						</view>
					</view>
					<!-- </u-swipe-action> -->
				</view>
			</view>
		</view>
		<u-button v-show="state == 1" @tap="add()" class="addBtn" type="success">添加收货地址</u-button>
		<u-button v-show="state == 0" @tap="add()" class="addBtn" type="success">添加发货地址</u-button>
		<view v-if="data == '' " class=" dataNone">
			<image src="../../static/image/dizhi@2x.png" mode=""></image>
			<p v-if="state == 1">您还没有收货地址哦 </p>
			<p v-if="state == 0">您还没有发货地址哦 </p>
		</view>
	</view>
</template>

<script>
	import {
		addresslist,
		addressDelete
	} from '@/api/api.js'
	export default {
		data() {
			return {
				data: [],
				state: 1,
				tab: 0,
				stopTab: 0,
				pageNumber: 1,
				pageSize: 10,
				stops: 0,
				disabled: false,
				show: false,
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#fa3534'
					}
				}]
			};
		},
		onLoad(e) {
			if (e.types != undefined) {
				this.state = e.types
			}
			if (e.tab != undefined) {
				this.tab = e.tab
				this.stopTab = 1
			}
		},
		onShow() {
			this.data = []
			this.stops = 0
			this.pageNumber = 1
			this.init()
		},
		onReachBottom() {
			if (this.stops == 0) {
				this.init()
			}
		},
		methods: {
			select(v) {
				if (this.stopTab == 0) {
					uni.setStorageSync('addressSet', v)
					this.$navigateTo('../CreateAddress/CreateAddress')
				} else {
					uni.setStorageSync('SelectAddress', v)
					uni.navigateBack()
				}

			},
			add() {
				let url = '../CreateAddress/CreateAddress?type=' + this.state
				this.$navigateTo(url)

			},
			GoSet(v) {
				uni.setStorageSync('addressSet', v)
				this.$navigateTo('../CreateAddress/CreateAddress')
			},
			navTab(state) {
				if (this.stopTab == 0) {
					this.data = []
					this.stops = 0
					this.state = state
					this.pageNumber = 1
					this.init()
				}
			},
			async init() {
				let paramrter = {
					type: this.state,
					pageNumber: this.pageNumber,
					pageSize: this.pageSize
				}
				let data = await addresslist(paramrter)
				data = data.data.list
				if (data.length != this.pageSize) {
					this.stops = 1
				}
				for (let i = 0; i < data.length; i++) {
					data[i].show = false
				}
				this.data = [...this.data, ...data]
				this.pageNumber = this.pageNumber+1
			},
			async remove(i, id) {
				this.data[i].show = false; 
				let data = await addressDelete({
					id: id
				})
				if (data.code == 0) {
					this.$u.toast(`删除成功`);
					this.data.splice(i, 1)
				}
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.data[index].show = true;
				this.data.map((val, idx) => {
					if (index != idx) this.data[idx].show = false;
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>
